//
//  TagView.m
//  XPhoto
//
//  Created by sentinel on 2020/4/20.
//  Copyright © 2020 baboy. All rights reserved.
//

#import "TagView.h"
#import "UIButton+tag.h"
#define TAG_MARGIN  5
@interface TagView()
@property(nonatomic, strong) NSMutableArray *buttons;
@end
@implementation TagView

- (NSMutableArray *)buttons{
    if(!_buttons){
        _buttons = [NSMutableArray array];
    }
    return _buttons;
}
- (UIButton *)addTag:(NSString*)tag type:(TagButtonType)type userInfo:(id)userInfo{
    UIImage *image = nil;
    if (type == TagButtonTypeSerial) {
        image = [UIImage imageNamed:@"location-mini"];
    }
    if (type == TagButtonTypeLocation) {
        image = [UIImage imageNamed:@"location-mini"];
    }
    if (type == TagButtonTypeLabel) {
        image = [UIImage imageNamed:@"location-mini"];
    }
    UIButton *btn = [UIButton buttonWithTag:tag image:image];
    btn.userInfo = userInfo;
    CGRect itemFrame = CGRectMake(self.contentInsets.left, self.contentInsets.top, 0, 0);
    [btn addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    itemFrame.size = btn.frame.size;
    if (self.buttons.count > 0) {
        UIButton *lastButton = self.buttons.lastObject;
        itemFrame.origin.x = lastButton.frame.origin.x + lastButton.frame.size.width + TAG_MARGIN;
        if ( (itemFrame.origin.x + itemFrame.size.width + self.contentInsets.right) > self.bounds.size.width ) {
            itemFrame.origin.y += itemFrame.size.height + TAG_MARGIN;
        }
        btn.frame = itemFrame;
    }
    [self.buttons addObject:btn];
    [self addSubview:btn];
    return btn;
}
- (CGSize)sizeThatFits:(CGSize)size{
    if([self.buttons count] == 0){
        return CGSizeMake(self.bounds.size.width, 0);
    }
    CGRect itemFrame = CGRectMake(self.contentInsets.left, self.contentInsets.top, 0, 0);
    for (NSInteger i = 0, n = self.buttons.count; i < n; i++) {
        UIButton *btn = [self.buttons objectAtIndex:i];
        itemFrame.size = btn.frame.size;
        itemFrame.size.width = MIN(itemFrame.size.width, self.bounds.size.width - self.contentInsets.left - self.contentInsets.right);
        if ( (itemFrame.origin.x + itemFrame.size.width + self.contentInsets.right) > self.bounds.size.width )  {
            itemFrame.origin.x = self.contentInsets.left;
            itemFrame.origin.y += itemFrame.size.height + TAG_MARGIN;
        }
        btn.frame = itemFrame;
        itemFrame.origin.x += itemFrame.size.width + TAG_MARGIN;
    }
    return CGSizeMake(self.bounds.size.width, itemFrame.origin.y + itemFrame.size.height);
}
- (void)clear{
    if (self.buttons) {
        [self.buttons makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self.buttons removeAllObjects];
    }
}
- (void)clickButton:(UIButton *)button{
    if (self.delegate && [self.delegate respondsToSelector:@selector(tagView:didSelectButton:userInfo:)]) {
        [self.delegate tagView:self didSelectButton:button userInfo:button.userInfo];
    }
}
@end
